#include "TFBondBbo.h"
#include <bondlib/BondContainer.h>
#include <uam/UserAccountManager.h>
#include <QJsonArray>
#include <QJsonObject>
#include <uam/ServerTimeMgr.h>
#include "qbtools/tools.h"
#include "../../BondUtility.h"

bool BondBboHandler::processCmd(const QJsonObject& cmdObj, const ReqInfo& reqinfo, bool unReg)
{
	parse(cmdObj);
	if (m_bonds.empty())
		return false;

	const CBrokerList& brokerList = SSUserAccountManager::instance().GetReadonlyBroker();
	CBondContainer& gc = CBondContainer::instance();
	int size = m_reqAll ? gc.GetSize() : (int)m_bonds.size();

	char Broker[8] = { 0 };
	int count = 0;
	QJsonArray list;
	QJsonObject jsRes;
	for (int i = 0; i < size; ++i)
	{
		const CBondInfo* pBond = m_reqAll ? gc.GetBond(i) : gc.GetBond(m_bonds[i]);
		if (!pBond || !pBond->IsValidBond())
			continue;

		for (auto iter = brokerList.GetBrokerMap().begin(); iter != brokerList.GetBrokerMap().end(); ++iter)
		{
			if(iter->first > 5)
				continue;
			FMTBUF(Broker, "%d", iter->first);
			REPORT rpt;
			if (!gc.GetCompanyRepByKey(pBond->GetCombBondKey(), Broker, rpt))
				continue;

			QJsonObject item;
			item["combkey"] = pBond->GetCombBondKey();
			item["code"] = pBond->GetBondCode();
			item["listedmarket"] = pBond->GetListedMarket();
			item["ttm"] = QString::fromLocal8Bit(pBond->GetRemainPeriod(ServerTimeMgr::instance().serverTime()));
			item["corp"] = Broker;

			bool bBidValid = true;
			bool bOfrValid = true;
			if (strcmp(rpt.m_bidinfo.m_price_status, "0") == 0)
			{
				bBidValid = false;
			}
			else
			{
				bBidValid = true;
			}

			if (strcmp(rpt.m_askinfo.m_price_status, "0") == 0)
			{
				bOfrValid = false;
			}
			else
			{
				bOfrValid = true;
			}

			// NAME://简称
			item["shortname"] = QString::fromLocal8Bit(pBond->GetBondShortName());
			//Vol.Bid
			if (!bBidValid)
			{
				item["bidvol"] = "--";
			}
			else
			{
				item["bidvol"] = SSTools::StringRemoveZero(rpt.m_bidinfo.m_sVolume);
			}

			// Bid
			if (bBidValid)
			{
				item["bid"] = displayPrice(&rpt.m_bidinfo);
			}
			else
			{
				item["bid"] = "--";
			}
			// OFR:
			if (bOfrValid)
			{
				item["ofr"] = displayPrice(&rpt.m_askinfo);
			}
			else
			{
				item["ofr"] = "--";
			}
			// vol.ofr
			if (!bOfrValid)
			{
				item["ofrvol"] = "--";
			}
			else
			{
				item["ofrvol"] = SSTools::StringRemoveZero(rpt.m_askinfo.m_sVolume);
			}

			item["updatetime"] = (int)rpt.m_time;

			list.append(item);

			if (++count % 20 == 0)
			{
				jsRes["finish"] = 0;
				jsRes["list"] = list;

				QJsonDocument doc(jsRes);
                emit sendDataToWeb(reqinfo.m_frameId, reqinfo.m_sid, doc.toJson(QJsonDocument::Compact), reqinfo.m_jsCb);

				QJsonObject emptyObj;
				QJsonArray emptyArr;
				jsRes.swap(emptyObj);
				list.swap(emptyArr);
			}
		}
	}
	jsRes["finish"] = 1;
	jsRes["list"] = list;

	QJsonDocument doc(jsRes);
    emit sendDataToWeb(reqinfo.m_frameId, reqinfo.m_sid, doc.toJson(QJsonDocument::Compact), reqinfo.m_jsCb);

	return true;
}

void BondBboHandler::parse(const QJsonObject& cmdObj)
{
	m_reqAll = false;
	QJsonObject condObj = cmdObj["cond"].toObject();
	if (condObj.isEmpty())
		return;

	QJsonArray keyList = condObj["list"].toArray();
	if (keyList.isEmpty())
	{
		m_reqAll = true;
		return;
	}
		

	for (auto item : keyList)
	{
		QJsonObject keyItem = item.toObject();
		CBondInfo bi;
		QString key = keyItem["combkey"].toString();

		int bondIndex = -1;
		if (!key.isEmpty())
		{
			bondIndex = CBondContainer::instance().GetBondIndex(key.toUtf8().data());
		}
		else if (!(key = keyItem["combcode"].toString()).isEmpty())
		{
			CBondContainer::instance().GetBondInfoByCombCode(key.toUtf8().data(), nullptr, &bondIndex);
		}
		if (!CBondContainer::instance().IsValidIndex(bondIndex))continue;
		m_bonds.push_back(bondIndex);
	}
}
